package com.wxy.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.common.core.controller.BaseController;
import com.wxy.common.core.domain.AjaxResult;
import com.wxy.common.utils.*;
import com.wxy.web.dto.UserDto;
import com.wxy.web.model.User;
import com.wxy.web.service.IRoleService;
import com.wxy.web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/admin/user")
public class UserController extends BaseController {
    @Autowired
    public DozerUtil dozerUtil;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @GetMapping("list")
    public AjaxResult list(User user)
    {
        Integer pageNum = ServletUtils.getParameterToInt("page",1);
        Integer pageSize = ServletUtils.getParameterToInt("limit",10);

        LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(user.getUsername()))
        {
            userLambdaQueryWrapper.like(User::getUsername , user.getUsername());
        }
        if(StringUtils.isNotEmpty(user.getFullname()))
        {
            userLambdaQueryWrapper.like(User::getFullname , user.getFullname());
        }

        Page<User> page = new Page<>(pageNum , pageSize);
        page.addOrder(OrderItem.desc("id"));
        IPage<User> iPage = userService.page(page, userLambdaQueryWrapper);

        HashMap<String,Object> map = new HashMap<>();
        map.put("list", iPage.getRecords());
        map.put("count", iPage.getTotal());

        AjaxResult result = AjaxResult.success();
        result.put("data",map);
        return result;
    }
    
    @GetMapping("detail")
    public AjaxResult detail()
    {
        String username = SecurityUtil.getUsername();
        HashMap<String,Object> map = userService.detail(username);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("data",map);
        return ajaxResult;
    }

    @PostMapping("logout")
    public AjaxResult logout(Integer id)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        return ajaxResult;
    }

    @GetMapping("info")
    public AjaxResult info(Integer id)
    {
        User user = userService.getById(id);
        UserDto userDto = dozerUtil.map(user, UserDto.class);
        userDto.setRoleids(roleService.listRoleIdsByUserId(id));
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put(AjaxResult.DATA_TAG,userDto);
        return ajaxResult;
    }
    
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody UserDto userDto)
    {
        User user = dozerUtil.map(userDto, User.class);
        userService.save(user);
        List<Integer> list = userDto.getRoleids();
        for (Integer roleId : list) {
            userService.insertUserRole(user.getId(),roleId);
        }
        AjaxResult ajaxResult = AjaxResult.success();
        return ajaxResult;
    }

    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody UserDto userDto)
    {
        User user = dozerUtil.map(userDto, User.class);
        userService.updateById(user);
        userService.deleteRoleByUserId(user.getId());
        List<Integer> list = userDto.getRoleids();
        for (Integer roleId : list) {
            userService.insertUserRole(user.getId(),roleId);
        }
        AjaxResult ajaxResult = AjaxResult.success();
        return ajaxResult;
    }

    @PostMapping("del/{id}")
    public AjaxResult del(@PathVariable Integer id)
    {
        userService.removeById(id);
        AjaxResult result = AjaxResult.success();
        return result;
    }

    @PostMapping("multiDelete")
    public AjaxResult multiDelete(@RequestBody List<Integer> ids)
    {
        userService.removeByIds(ids);
        AjaxResult result = AjaxResult.success();
        return result;
    }

    @PostMapping("pwd")
    public AjaxResult pwd(@Validated @RequestBody User user)
    {
        user.setPassword(EncryptUtil.encryptPassword(user.getPassword()));
        userService.updateById(user);
        AjaxResult ajaxResult = AjaxResult.success();
        return ajaxResult;
    }
}
